{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Single-Row Keyboard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: calculateTime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #单行键盘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>我们定制了一款特殊的键盘，所有的键都 <strong>排列在一行上</strong>&nbsp;。</p>\n",
    "\n",
    "<p>给定一个长度为 <code>26</code> 的字符串&nbsp;<code>keyboard</code>&nbsp;，来表示键盘的布局(索引从 <code>0</code> 到 <code>25</code> )。一开始，你的手指在索引 <code>0</code> 处。要输入一个字符，你必须把你的手指移动到所需字符的索引处。手指从索引&nbsp;<code>i</code>&nbsp;移动到索引&nbsp;<code>j</code>&nbsp;所需要的时间是&nbsp;<code>|i - j|</code>。</p>\n",
    "\n",
    "<p>您需要输入一个字符串&nbsp;<code>word</code>&nbsp;。写一个函数来计算用一个手指输入需要多少时间。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>keyboard = \"abcdefghijklmnopqrstuvwxyz\", word = \"cba\"\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>从 0 号键移动到 2 号键来输出 'c'，又移动到 1 号键来输出 'b'，接着移动到 0 号键来输出 'a'。\n",
    "总用时 = 2 + 1 + 1 = 4. \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>keyboard = \"pqrstuvwxyzabcdefghijklmno\", word = \"leetcode\"\n",
    "<strong>输出：</strong>73\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>keyboard.length == 26</code></li>\n",
    "\t<li><code>keyboard</code>&nbsp;按某种特定顺序排列，并包含每个小写英文字母一次。</li>\n",
    "\t<li><code>1 &lt;= word.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>word[i]</code>&nbsp;为小写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [single-row-keyboard](https://leetcode.cn/problems/single-row-keyboard/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [single-row-keyboard](https://leetcode.cn/problems/single-row-keyboard/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abcdefghijklmnopqrstuvwxyz\"\\n\"cba\"', '\"pqrstuvwxyzabcdefghijklmno\"\\n\"leetcode\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        s = {}\n",
    "        for i,w in enumerate(keyboard):\n",
    "            s[w]=i\n",
    "        last,res = 0,0\n",
    "        for w in word:\n",
    "            res+=abs(s[w]-last)\n",
    "            last=s[w]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        key_idx_map = dict(zip(list(keyboard),list(range(len(keyboard)))))\n",
    "\n",
    "        res = prev = 0\n",
    "        for cur in word:\n",
    "            res += abs(key_idx_map[cur] - prev)\n",
    "            prev = key_idx_map[cur]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        hs = {x:i for i,x in enumerate(keyboard)}\n",
    "        i = 0\n",
    "        step = 0\n",
    "        for s in word:\n",
    "            step += abs(hs[s] - i)\n",
    "            i = hs[s]\n",
    "        return step\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        dic={}\n",
    "        now=0\n",
    "        for i in keyboard:\n",
    "            dic[i]=now \n",
    "            now+=1\n",
    "        res=0\n",
    "        l=len(word)\n",
    "        print(dic)\n",
    "        res=dic[word[0]]\n",
    "        for i in range(l-1):\n",
    "            res+=abs(dic[word[i+1]]-dic[word[i]])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        ind = {w: i for i, w in enumerate(keyboard)}\n",
    "        ans = pre = 0\n",
    "        for w in word:\n",
    "            ans += abs(pre-ind[w])\n",
    "            pre = ind[w]\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        res = 0\n",
    "        id = 0\n",
    "        for x in word:\n",
    "            ans = keyboard.index(x)\n",
    "            res += abs(ans-id)\n",
    "            id = ans\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        keyboard_wor_pos = {}\n",
    "        for idx, w in enumerate(keyboard):\n",
    "            keyboard_wor_pos[w] = idx\n",
    "\n",
    "        res = 0\n",
    "        current_position = 0\n",
    "        for w in word:\n",
    "            distance = abs(keyboard_wor_pos[w] - current_position)\n",
    "            res += distance\n",
    "            current_position = keyboard_wor_pos[w]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        d = {}\n",
    "        for i in range(len(keyboard)):\n",
    "            d[keyboard[i]] = i\n",
    "        a = [d[x] for x in word]\n",
    "        r = a[0]\n",
    "        for i in range(1, len(a)):\n",
    "            r += abs(a[i]-a[i-1])\n",
    "        return r\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        char_to_index = {char: index for index, char in enumerate(keyboard)}\n",
    "\n",
    "        def get_distance(start_word, end_word):\n",
    "            return abs(char_to_index[start_word] - char_to_index[end_word])\n",
    "        \n",
    "        start_word = keyboard[0]\n",
    "        distance_sum = 0\n",
    "        for end_word in word:\n",
    "            distance_sum += get_distance(start_word, end_word)\n",
    "            start_word = end_word\n",
    "        \n",
    "        return distance_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        ans = 0\n",
    "        pre = 0\n",
    "        for c in word:\n",
    "            p = keyboard.index(c)\n",
    "            ans += abs(p - pre)\n",
    "            pre = p\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        time=0\n",
    "        loca=0\n",
    "        for i in word:\n",
    "            time += abs(keyboard.index(i)-loca)\n",
    "            loca=keyboard.index(i)\n",
    "        return time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        hashmap = {}\n",
    "        count = 0\n",
    "\n",
    "        pivot = 0\n",
    "        for i in range(len(keyboard)):\n",
    "            hashmap[keyboard[i]] = i\n",
    "\n",
    "        for i in word:\n",
    "            count += abs(pivot - hashmap[i])\n",
    "            pivot = hashmap[i]\n",
    "\n",
    "        return count \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        ans=0\n",
    "        dic={x:i for i,x in enumerate(keyboard)}\n",
    "        ans+=dic[word[0]]\n",
    "        for i in range(1, len(word)):\n",
    "            ans+=abs(dic[word[i]]-dic[word[i-1]])\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        d = dict()\n",
    "        for i in range(26):\n",
    "            d[keyboard[i]] = i\n",
    "\n",
    "        ans, last = 0, keyboard[0]\n",
    "        for c in word:\n",
    "            ans += abs(d[c] - d[last])\n",
    "            last = c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        res = 0\n",
    "        dic = collections.defaultdict(int)\n",
    "        for i in range(len(keyboard)):\n",
    "            dic[keyboard[i]] = i\n",
    "        pre = 0\n",
    "        for s in word:\n",
    "            res += abs(dic[s] - pre)\n",
    "            pre = dic[s]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        keyboard_mapper = {key: index for index, key in enumerate(keyboard)}\n",
    "\n",
    "        current_char = word[0]\n",
    "        result = keyboard_mapper[current_char]\n",
    "\n",
    "        for index in range(1, len(word)):\n",
    "            char = word[index]\n",
    "            result += abs(keyboard_mapper[current_char] - keyboard_mapper[char])\n",
    "            current_char = char\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        # keyboard_wor_pos = {}\n",
    "        # for idx, w in enumerate(keyboard):\n",
    "        #     keyboard_wor_pos[w] = idx\n",
    "\n",
    "        # res = 0\n",
    "        # current_position = 0\n",
    "        # for w in word:\n",
    "        #     distance = abs(keyboard_wor_pos[w] - current_position)\n",
    "        #     res += distance\n",
    "        #     current_position = keyboard_wor_pos[w]\n",
    "\n",
    "        # return res\n",
    "        \n",
    "        keyboard_wor_pos = [0] * 26\n",
    "        for idx, w in enumerate(keyboard):\n",
    "            keyboard_wor_pos[ord(w) - ord('a')] = idx\n",
    "\n",
    "        res = 0\n",
    "        current_position = 0\n",
    "        for w in word:\n",
    "            distance = abs(keyboard_wor_pos[ord(w) - ord('a')] - current_position)\n",
    "            res += distance\n",
    "            current_position = keyboard_wor_pos[ord(w) - ord('a')]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        pos_dict = {c: i for i, c in enumerate(keyboard)}\n",
    "        ans = 0 \n",
    "        pre_word = keyboard[0]\n",
    "        for i in range(len(word)):\n",
    "            ans += abs(pos_dict[word[i]] - pos_dict[pre_word])\n",
    "            pre_word = word[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        pre = 0\n",
    "        n = 0\n",
    "        for ch in word:\n",
    "            idx = keyboard.index(ch)\n",
    "            n += abs(idx - pre)\n",
    "            pre = idx\n",
    "        \n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 解法 1\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        index=0\n",
    "        count=0\n",
    "        for i in word:\n",
    "            count+=abs(index-keyboard.index(i))\n",
    "            index=keyboard.index(i)\n",
    "        return count\n",
    "\n",
    "#  解法 2\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        res=0\n",
    "        dic={}\n",
    "        idx=0\n",
    "        for index,value in enumerate(keyboard):\n",
    "            dic[value]=index\n",
    "        for i in word:\n",
    "            time=abs(idx-dic[i])\n",
    "            res+=time\n",
    "            idx=dic[i]\n",
    "        print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "        \n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        char_map = dict()\n",
    "        for i, char in enumerate(keyboard):\n",
    "            char_map[char] = i\n",
    "\n",
    "        cur_posi, step = 0, 0\n",
    "        for char in word:\n",
    "            step += abs(cur_posi - char_map[char])\n",
    "            cur_posi = char_map[char]\n",
    "        \n",
    "        return step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        loc = 0\n",
    "        len_max = 0\n",
    "        for i in range(len(word)):\n",
    "            index = keyboard.index(word[i])\n",
    "            len_max  += abs(index-loc)\n",
    "            loc = index\n",
    "        return len_max\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        ans = keyboard.index(word[0])\n",
    "        for i in range(1, len(word)):\n",
    "            ans += abs(keyboard.index(word[i]) - keyboard.index(word[i - 1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        sum = 0\n",
    "        cnt = 0\n",
    "        wlen = len(word)\n",
    "        klen = len(keyboard)\n",
    "        for i in range(wlen):\n",
    "            for j in range(klen):\n",
    "                if word[i] == keyboard[j] :\n",
    "                    sum += abs(j - cnt)\n",
    "                    cnt = j\n",
    "                    break\n",
    "        return sum\n",
    "                        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        m = {c: i for i, c in enumerate(keyboard)}\n",
    "        \n",
    "        ret = 0\n",
    "        i = 0\n",
    "\n",
    "        for c in word:\n",
    "            ret += abs(m[c] - i)\n",
    "            i = m[c]\n",
    "        \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        d = dict([(ele,i) for i, ele in enumerate(keyboard)])\n",
    "        p = 0\n",
    "        cnt = 0\n",
    "        for c in word:\n",
    "            cur = d[c]\n",
    "            cnt += abs(cur-p)\n",
    "            p = cur\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        hashmap = {c: idx for idx, c in enumerate(keyboard)}\n",
    "\n",
    "        i, res = 0, 0\n",
    "        for c in word:\n",
    "            j = hashmap[c]\n",
    "            res += abs(i -j)\n",
    "            i = j\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        kbd = dict()\n",
    "        for i in range(len(keyboard)):\n",
    "            kbd[keyboard[i]] = i\n",
    "\n",
    "        cur = 0\n",
    "        dist = 0\n",
    "        for w in word:\n",
    "            idx = kbd[w]\n",
    "            dist += cur - idx if cur >= idx else idx - cur\n",
    "            cur = idx\n",
    "        return dist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "\n",
    "        # pre_key = word[0]\n",
    "        hashmap = {}\n",
    "        for i in range(len(keyboard)):\n",
    "            hashmap[keyboard[i]] = i\n",
    "        time_result = abs(hashmap[word[0]])\n",
    "        n = len(word)\n",
    "        if n == 1:\n",
    "            return time_result\n",
    "        # print(hashmap)\n",
    "        for i in range(1, n):\n",
    "            time_result += abs(hashmap[word[i]] - hashmap[word[i-1]])\n",
    "        return time_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        dt=dict()\n",
    "        for i in range(len(keyboard)):\n",
    "            dt[keyboard[i]]=i\n",
    "        ans=0\n",
    "        begin =0\n",
    "        for i in range(len(word)):\n",
    "            target=dt[ word[i]]\n",
    "            ans+=abs(target-begin)\n",
    "            begin=target\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        c2index = dict()\n",
    "        for index, c in enumerate(keyboard):\n",
    "            c2index[c] = index\n",
    "        \n",
    "        count = 0\n",
    "        start = 0\n",
    "        for c in word:\n",
    "            count += abs(c2index[c]-start)\n",
    "            start = c2index[c]\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        time=0\n",
    "        loca=0\n",
    "        for j in range(len(word)):\n",
    "            for i,v in enumerate(keyboard):\n",
    "                if v==word[j]:\n",
    "                    time+=abs(i-loca)\n",
    "                    loca=i\n",
    "        return time\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        d ={}\n",
    "        for i, ch in enumerate(keyboard):\n",
    "            d[ch] = i \n",
    "        cur, step = 0, 0\n",
    "        for c in word:\n",
    "            step += abs(cur-d[c])\n",
    "            cur = d[c]\n",
    "        return step        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        keys = {}\n",
    "        for i in range(len(keyboard)):\n",
    "            keys[keyboard[i]] = i\n",
    "        res = 0\n",
    "        pos = 0\n",
    "        for i in range(len(word)):\n",
    "            temp = keys[word[i]]\n",
    "            res += abs(temp-pos)\n",
    "            pos = temp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        dic = {}\n",
    "        n = len(keyboard)\n",
    "        for id, x in enumerate(keyboard):\n",
    "            dic[x] = id\n",
    "        store = 0\n",
    "        res = 0\n",
    "        for i in word:\n",
    "            res += abs(dic[i]-store)\n",
    "            store = dic[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        Su,d = 0,0\n",
    "        for i in word:\n",
    "            Su += abs(keyboard.find(i)-d)\n",
    "            d = keyboard.find(i)\n",
    "        return Su"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        d = collections.defaultdict(int)\n",
    "        for i, c in enumerate(keyboard):\n",
    "            d[c] = i\n",
    "\n",
    "        ret = 0\n",
    "        x = 0\n",
    "        for c in word:\n",
    "            i = d[c]\n",
    "            ret += abs(i - x)\n",
    "            x = i\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        timecount=[]\n",
    "        for i in range(len(word)):\n",
    "            timecount.append(keyboard.find(word[i]))\n",
    "\n",
    "        # print(timecount)\n",
    "        res = sum(abs(timecount[i+1]-timecount[i]) for i in range(len(timecount)-1)) + timecount[0]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        idx_list = [x for x in range(len(keyboard))]\n",
    "        ki_dict = dict(zip(list(keyboard), idx_list))\n",
    "        times = 0\n",
    "        start = 0\n",
    "        i = 0\n",
    "        while i < len(word):\n",
    "            times += abs(ki_dict[word[i]] - start)\n",
    "            start = ki_dict[word[i]]\n",
    "            i += 1\n",
    "        return times\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        word_idx = {j: i for i, j in enumerate(keyboard)}\n",
    "        mv = 0\n",
    "        pre = 0\n",
    "        for w in word:\n",
    "            cur = word_idx[w]\n",
    "            mv += abs(cur-pre)\n",
    "            pre = cur\n",
    "        return mv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculateTime(self, keyboard: str, word: str) -> int:\n",
    "        d = dict()\n",
    "        for i in range(26):\n",
    "            for j in range(i + 1, 26):\n",
    "                d[(keyboard[i], keyboard[j])] = j - i\n",
    "                d[(keyboard[j], keyboard[i])] = j - i\n",
    "\n",
    "        ans, last = 0, keyboard[0]\n",
    "        for c in word:\n",
    "            ans += d.get((last, c), 0)\n",
    "            last = c\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
